package com.xzx.framework.util;

import java.math.BigDecimal;
import java.util.Arrays;
import java.util.List;
import java.util.Random;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * 通用工具类
* @ClassName: CommonUtil    
* @Description:     
* @author xzx   
* @date 2018年3月7日 下午5:13:50    
*
 */
public abstract class CommonUtil {

	private final static String[] strs = { "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O",
			"P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z", "0", "1", "2", "3", "4", "5", "6", "7", "8", "9" };

	/**
	 * 取得指定长度的随机码
	 * 
	 * @param length
	 * @return
	 */
	public static String getRandCode(int length) {
		Random random = new Random();
		StringBuffer buf = new StringBuffer("");
		for (int i = 0; i < length; i++) {
			buf.append(strs[random.nextInt(strs.length)]);
		}
		return buf.toString();
	}

	/**
	 * 是否是Email
	 * 
	 * @param email
	 * @return
	 */
	public static boolean isEmail(String email) {
		if (email == null || email.trim().equals("")) {
			return false;
		}
		String regex = "\\w+([-+.]\\w+)*@\\w+([-.]\\w+)*\\.\\w+([-.]\\w+)*";
		Pattern p = Pattern.compile(regex);
		Matcher m = p.matcher(email);
		return m.find();
	}

	/**
	 * 是否是手机号码
	 * 
	 * @param mobile
	 * @return
	 */
	public static boolean isMobile(String mobile) {
		if (mobile == null || mobile.trim().equals("")) {
			return false;
		}
		Pattern p = Pattern.compile("^[1][3-8]\\d{9}$");
		Matcher m = p.matcher(mobile);
		return m.find();
	}

	/**
	 * 是否是电话号码
	 * 
	 * @param phone
	 * @return
	 */
	public static boolean isPhone(String phone) {
		if (phone == null || phone.trim().equals("")) {
			return false;
		}
		Pattern p = Pattern.compile("^[\\d]{5,20}$");
		Matcher m = p.matcher(phone);
		return m.find();
	}

	/**
	 * 按指定长度截取字符串(一个汉字相当于两个字符)，超过的以省略号代替
	 * 
	 * @param str
	 * @param len
	 * @return
	 */
	public static String cutString(String str, int len) {
		if (str == null || str.length() <= 0 || len <= 0)
			return "";

		StringBuffer buf = new StringBuffer("");
		int count = 0;
		for (int i = 0; i < str.length(); i++) {
			char c = str.charAt(i);
			count += isChineseChar(c) ? 2 : 1;

			if (count > len) {
				buf.append("......");
				break;
			}
			buf.append(c);
		}

		return buf.toString();
	}

	/**
	 * 以纯文本形式显示时的过滤逻辑
	 * 
	 * @param str
	 * @return
	 */
	public static String escape(String str) {
		if (str == null || str.length() <= 0)
			return "";

		return str.trim().replace("<", "&lt;").replace(">", "&gt;").replace("\"", "\\\"").replace("\r\n", "<br/>")
				.replace("\n", "<br/>");
	}

	/**
	 * 需要显示html标签时的过滤逻辑
	 * 
	 * @param str
	 * @return
	 */
	public static String escape2(String str) {
		if (str == null || str.length() <= 0)
			return "";

		return str.trim().replace("\"", "\\\"").replace("\r\n", "<br/>").replace("\n", "<br/>");
	}

	/**
	 * 是否是中文字符
	 * 
	 * @param c
	 * @return
	 */
	public static boolean isChineseChar(char c) {
		return Character.isLetter(c) && c > 255;
	}

	/**
	 * 数组array中是否包含元素n
	 * 
	 * @param array
	 * @param n
	 * @return
	 */
	public static boolean exists(Integer[] array, int n) {
		if (array == null || array.length <= 0)
			return false;
		for (int _n : array) {
			if (n == _n) {
				return true;
			}
		}
		return false;
	}

	/**
	 * 集合list中是否包含元素str
	 * 
	 * @param list
	 * @param str
	 * @return
	 */
	public static boolean exists(List<String> list, String str) {
		if (list == null || str == null) {
			throw new RuntimeException();
		}

		for (String astr : list) {
			if (astr.trim().equals(str.trim())) {
				return true;
			}
		}
		return false;
	}

	/**
	 * 判断对象是否为空
	 * 
	 * @param object
	 *            被判断的对象
	 * @return 判断结果，空为ture，否则为false
	 */
	public static boolean isNull(Object object) {
		boolean isNull = false;
		if (!(object + "").equals("0")) {
			isNull = object == null || object.toString().trim().length() == 0 || object.toString().matches("^[0\\.]+$")
					|| object.toString().equals("null");
		}
		return isNull;
	}

	public static String null2String(Object s) {
		return s == null ? "" : s.toString().trim();
	}
	
	
	/**
	 * 随机数生成
	 * 
	 * @param n
	 *            随机数个数
	 */
	public static String randomCommon(int n) {
		Random random = new Random();
		String result = "";
		for (int i = 0; i < n; i++) {
			result += random.nextInt(10);
		}
		return result;
	}

	/**
	 * 用户手机号修饰功能，返回187****1234格式
	 * @param phone
	 * @return
	 */
	public static String decoratePhoneNum(String phone){
		char [] phoneArr = phone.toCharArray();
	      StringBuffer buffer = new StringBuffer();
	      for (int i = 0; i < phoneArr.length; i++) {
	        if(i>2&&i<7){
	          buffer.append("*");
	        }else{
	          buffer.append(phoneArr[i]);
	        }
	    }
	      return buffer.toString();
	}
	
	/**
	 * String转int
	 * 
	 * @param s
	 * @return
	 */
	public static int nullInt(Object s) {
		int v = 0;
		if (!CommonUtil.isNull(s))
			try {
				v = Integer.parseInt(s.toString());
			} catch (Exception localException) {
				localException.printStackTrace();
			}
		return v;
	}

	/**
	 * String转float
	 * 
	 * @param s
	 * @return
	 */
	public static float nullFloat(Object s) {
		float v = 0.0F;
		if (!CommonUtil.isNull(s))
			try {
				v = Float.parseFloat(s.toString());
			} catch (Exception localException) {
			}
		return v;
	}

	/**
	 * String转Double
	 * 
	 * @param s
	 * @return
	 */
	public static double nullDouble(Object s) {
		double v = 0.0D;
		if (!CommonUtil.isNull(s))
			try {
				v = Double.parseDouble(nullString(s));
			} catch (Exception localException) {
			}
		return v;
	}

	public static String nullString(Object s) {
		return s == null ? "" : s.toString().trim();
	}

	/**
	 * String转BigDecimal
	 * 
	 * @param s
	 * @return
	 */
	public static BigDecimal nullBigDecimal(Object s) {
		BigDecimal decs = new BigDecimal("0");
		if (!CommonUtil.isNull(s)) {
			decs = new BigDecimal(s.toString());
		}
		return decs;
	}

	/**
	 * String转Long
	 * 
	 * @param s
	 * @return
	 */
	public static Long nullLong(Object s) {
		Long v = Long.valueOf(0L);
		if (s != null) {
			try {
				v = Long.parseLong(s.toString());
			} catch (Exception localException) {
			}
		}
		return v;
	}

	/** 数组升序排序 */
	public static String sortArray(String arrayStr) {
		String[] array = arrayStr.split(",");
		Arrays.sort(array);
		String sortAfter = "";
		for (int i = 0; i < array.length; i++) {
			sortAfter += array[i] + ",";
		}
		return sortAfter;
	}

    /** 
     * 经纬度距离算法
     * @param long1
     * @param lat1
     * @param long2
     * @param lat2
     * @return
     */
    public static double distance(double long1, double lat1, double long2,  
            double lat2) {  
        double a, b, R;  
        R = 6378137; // 地球半径  
        lat1 = lat1 * Math.PI / 180.0;  
        lat2 = lat2 * Math.PI / 180.0;  
        a = lat1 - lat2;  
        b = (long1 - long2) * Math.PI / 180.0;  
        double d;  
        double sa2, sb2;  
        sa2 = Math.sin(a / 2.0);  
        sb2 = Math.sin(b / 2.0);  
        d = 2  
                * R  
                * Math.asin(Math.sqrt(sa2 * sa2 + Math.cos(lat1)  
                        * Math.cos(lat2) * sb2 * sb2));  
        return d;  
    } 
    /**
     * 用户姓名修饰功能，完整姓名修饰后：章**
     * @param name
     * @return
     */
    
    public static String decorateName(String name){
	    if (null!=name&&name.length() > 1&&!"null".equals(name)) {  
	        StringBuffer stringBuffer = new StringBuffer();  
	        for (int i = 0; i < (name.length() - 1); i++) {  
	            stringBuffer.append("*");  
	        }  
	        return name.replaceAll("([\\u4e00-\\u9fa5]{1})(.*)", "$1" + stringBuffer.toString()); 
	    } else if(null!=name&&name.length()== 1&&!"null".equals(name)){
	      return name;
	    }
	      return "";
    }
    
    public static void main(String[] args) throws Exception {
		//Date date = DateUtil.parseDate("2017-01-06 19:00:00", DateUtil.PATTERN_DATE_TIME2);
		//System.out.println(compareDate(date, 1L));
    	//116.440632,39.930401 朝阳门
    	//116.442285,39.914686 建国门
    	//System.out.println(distance(116.440632, 39.930401, 116.442285, 39.914686));
    	System.out.println(getRandCode(8));
	}
}
